/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package boundary.management;

import components.TabPanel;
import components.utils.ComponentGroup;
import components.utils.ListSelectionEnabler;
import components.utils.observer.ListUpdater;
import control.Application;
import entity.DatabaseTest;
import entity.Entity;
import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;

/**
 *
 * @author Juan
 */
public class PTestManagement extends TabPanel {
    
    private ComponentGroup listControls = new ComponentGroup();
    private ListUpdater testUpdater;

    /**
     * Creates new form PTestManagement
     */
    public PTestManagement() {
        initComponents();
        
        listControls.addComponent(BMoreInfo);
        listControls.addComponent(BDeleteAnalysis);
        
        LTests.addListSelectionListener(new ListSelectionEnabler(listControls, LTests));
        
        testUpdater =
                new ListUpdater(Application.DATABASE_TEST_MANAGER.getCollection(), LTests);
        Application.DATABASE_TEST_MANAGER.addObserver(testUpdater);
        testUpdater.notifyObserver();
    }

    @Override
    protected void removeObservers() {
        Application.DATABASE_TEST_MANAGER.removeObserver(testUpdater);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        LTests = new javax.swing.JList();
        BMoreInfo = new javax.swing.JButton();
        BDeleteAnalysis = new javax.swing.JButton();
        BRegister = new javax.swing.JButton();
        BClose = new javax.swing.JButton();

        jLabel1.setText("Análisis registrados:");

        LTests.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                LTestsMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(LTests);

        BMoreInfo.setText("Ver Información");
        BMoreInfo.setEnabled(false);

        BDeleteAnalysis.setText("Borrar Análisis");
        BDeleteAnalysis.setEnabled(false);
        BDeleteAnalysis.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BDeleteAnalysisActionPerformed(evt);
            }
        });

        BRegister.setText("Registrar Nuevo Análisis");
        BRegister.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BRegisterActionPerformed(evt);
            }
        });

        BClose.setText("Cerrar");
        BClose.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BCloseActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(BMoreInfo)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(BDeleteAnalysis))
                            .addComponent(BRegister)
                            .addComponent(BClose))
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(BMoreInfo)
                    .addComponent(BDeleteAnalysis))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(BRegister)
                .addGap(18, 18, 18)
                .addComponent(BClose)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void BCloseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BCloseActionPerformed
        buttonTabComponent.closeTab();
    }//GEN-LAST:event_BCloseActionPerformed

    private void LTestsMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_LTestsMouseClicked
        DatabaseTest databaseTest = (DatabaseTest) LTests.getSelectedValue();
        if (evt.getClickCount() == 2 && databaseTest != null) {
            Application.displayEntity(databaseTest);
        }
    }//GEN-LAST:event_LTestsMouseClicked

    private void BDeleteAnalysisActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BDeleteAnalysisActionPerformed
        int selectedIndex = LTests.getSelectedIndex();
        if (selectedIndex != -1) {
            DefaultListModel<DatabaseTest> model = (DefaultListModel<DatabaseTest>) LTests.getModel();
            DatabaseTest databaseTest = model.getElementAt(selectedIndex);
            int option = JOptionPane.showConfirmDialog(getTopLevelAncestor(),
                    "¿Está seguro que desea eliminar el análisis " + databaseTest + "?",
                    "Advertencia", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE);
            if (option == JOptionPane.YES_OPTION) {
                model.remove(selectedIndex);
                if (selectedIndex == 0) {
                    if (model.getSize() == 0) {
                        LTests.setSelectedIndex(-1);
                    } else {
                        LTests.setSelectedIndex(0);
                    }
                } else {
                    LTests.setSelectedIndex(--selectedIndex);
                }
            }
        }
    }//GEN-LAST:event_BDeleteAnalysisActionPerformed

    private void BRegisterActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BRegisterActionPerformed
        Application.F_MAIN.openTestRegistrationPanel();
    }//GEN-LAST:event_BRegisterActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton BClose;
    private javax.swing.JButton BDeleteAnalysis;
    private javax.swing.JButton BMoreInfo;
    private javax.swing.JButton BRegister;
    private javax.swing.JList LTests;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables
}
